//
//  ViewController.m
//  RunLoop2
//
//  Created by 崔博 on 2018/8/13.
//  Copyright © 2018年 崔博. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

//kCFRunLoopEntry = (1UL << 0),
//kCFRunLoopBeforeTimers = (1UL << 1),
//kCFRunLoopBeforeSources = (1UL << 2),
//kCFRunLoopBeforeWaiting = (1UL << 5),
//kCFRunLoopAfterWaiting = (1UL << 6),
//kCFRunLoopExit = (1UL << 7),

void runloopObversActivitier(CFRunLoopObserverRef obsever,CFRunLoopActivity activity, void * info)
{
    switch (activity) {
        case kCFRunLoopEntry:
            NSLog(@"kCFRunLoopEntry---%@",CFRunLoopCopyCurrentMode(CFRunLoopGetCurrent()));
            break;
        case kCFRunLoopBeforeTimers:
            NSLog(@"kCFRunLoopBeforeTimers");
            break;
        case kCFRunLoopBeforeSources:
            NSLog(@"kCFRunLoopBeforeSources");
            break;
        case kCFRunLoopBeforeWaiting:
            NSLog(@"kCFRunLoopBeforeWaiting");
            break;
        case kCFRunLoopAfterWaiting:
            NSLog(@"kCFRunLoopAfterWaiting");
            break;
        case kCFRunLoopExit:
            NSLog(@"kCFRunLoopExit");
            break;
            
        default:
            break;
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 拿到Runloop对象
    [NSRunLoop currentRunLoop];
    CFRunLoopGetCurrent();
    // 默认defaut traching
    
    // 创建obsever
    
    CFRunLoopObserverRef ob = CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopAllActivities, YES, 0, runloopObversActivitier, NULL);
    // 添加obsever 到runloop中
    CFRunLoopAddObserver(CFRunLoopGetMain(), ob, kCFRunLoopCommonModes);
    CFRelease(ob);
    CFRunLoopObserverCreateWithHandler(kCFAllocatorDefault, kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
        switch (activity) {
            case kCFRunLoopEntry:{
                CFRunLoopMode mod = CFRunLoopCopyCurrentMode(CFRunLoopGetCurrent());
                NSLog(@"kCFRunLoopEntry---%@",CFRunLoopCopyCurrentMode(CFRunLoopGetCurrent()));
                CFRelease(mod);
            }
                break;
            case kCFRunLoopExit:
                NSLog(@"kCFRunLoopExit");
                break;
                
            default:
                break;
        }
    });
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [NSTimer scheduledTimerWithTimeInterval:5 repeats:NO block:^(NSTimer * _Nonnull timer) {
        NSLog(@"定时器");
    }];
}
@end
